package com.zy.cat.controller.dev.v1;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zy.cat.common.annotation.CheckDevPermission;
import com.zy.cat.common.annotation.CheckingMD5;
import com.zy.cat.common.utils.DateAPIUtils;
import com.zy.cat.common.utils.TJKUtils;
import com.zy.cat.dao.Dao;
import com.zy.cat.entity.dev.CatDevReRedHistory;
import com.zy.cat.entity.dev.CatDevUserInfo;
import com.zy.cat.entity.dev.repository.CatDevReRedHistoryRepository;
import com.zy.cat.entity.dev.resp.CatDevOrderResp;
import com.zy.cat.entity.dev.resp.CatDevReRedHistoryResp;
import com.zy.cat.entity.dev.resp.CatDevRebateInfoResp;
import com.zy.cat.entity.resp.Pages;
import com.zy.cat.entity.resp.RSPE;
import com.zy.cat.entity.resp.ResultPage;
import com.zy.cat.service.CacheService;
import com.zy.cat.service.CatDevService;
import com.zy.cat.service.CatOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value = "9CatDevOrder", tags = { "9趣猫用户订单信息开放API" }, description = "9趣猫用户订单信息开放接口")
@RestController
@RequestMapping("/v1/user/order")
@Slf4j
public class CatDevOrderController {
	@Autowired
	CatOrderService catOrderService;

	@Autowired
	CacheService cacheService;
	
	@Autowired
	CatDevService catDevService;
	
	@Autowired
	private CatDevReRedHistoryRepository catDevReRedHistoryRepository;
	
	@Autowired 
	private Dao<CatDevOrderResp> orderRespDao;

	@PostMapping("/list")
	@ApiOperation(value = "9趣猫用户个人订单信息", notes = "仅显示个人订单信息。每月25结算上月收入，25号~月底为提现日", consumes = "application/x-www-form-urlencoded", position = 1, response = CatDevOrderResp.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "dstatus", value = "订单状态（-1：已失效、0：入帐中 、1.已入账、10.全部)", example = "10", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "otype", value = "订单类型（0.淘宝、1.拼多多、2.京东、10.全部）", example = "10", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_dstatus_pid_utoken_uuid_dstatus_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckDevPermission
	@CheckingMD5(checkingStr = "rtype_dstatus_pid_utoken_uuid_dstatus", MD5 = "token")
	public Object list(@RequestParam(name="otype",defaultValue = "10") Integer otype,Integer rtype, String pid, Integer dstatus, String utoken, String uuid, String token,
			Pages pages) {
		if (dstatus == null || dstatus < -1 || (dstatus > 1 && dstatus != 10)) {
			return new RSPE<ResultPage<CatDevOrderResp>>(-2, "订单状态不合法！");
		}
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-3, "应用平台参数错误！");
		}
		Callable<RSPE<ResultPage<CatDevOrderResp>>> result = new Callable<RSPE<ResultPage<CatDevOrderResp>>>() {
			@Override
			public RSPE<ResultPage<CatDevOrderResp>> call() throws Exception {
				return new RSPE<ResultPage<CatDevOrderResp>>(catOrderService.list(pid, dstatus,otype, pages));
			}
		};
		return result;
	}

	@PostMapping("/add")
	@ApiOperation(value = "9趣猫用户手动填单", position = 2, notes = "趣猫用户手动填单", consumes = "application/x-www-form-urlencoded", response = RSPE.class, responseContainer = "Map")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "tid", value = "订单号", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "date", value = "下单日期", example = "2019-12-20", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_date_tid_pid_utoken_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckDevPermission
	@CheckingMD5(checkingStr = "rtype_uuid_date_tid_pid_utoken", MD5 = "token")
	public Object addOrder(Integer rtype, String pid, String tid,String date, String uuid, String utoken, String token) {
		if (StringUtils.isBlank(tid) || tid.length() < 10) {
			return new RSPE<Object>(-2, "订单号不合法！");
		}
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-3, "应用平台参数错误！");
		}
		//校验时间格式是否正确
		if (!DateAPIUtils.isFormat(date, DateAPIUtils.YYYYMMDD)) {
			return new RSPE<Object>(-4, "时间格式错误！");
		}
		CatDevOrderResp orderResp = catOrderService.getOrderById(tid);
		if (orderResp!=null) {
			return new RSPE<Object>(-5, "该订单号已存在！");
		}
		Callable<RSPE<Boolean>> result = new Callable<RSPE<Boolean>>() {
			@Override
			public RSPE<Boolean> call() throws Exception {
				return new RSPE<Boolean>(catOrderService.addOrder(pid, tid,date));
			}
		};
		return result;
	}
	
	@PostMapping("/show/type")
	@ApiOperation(value = "9趣猫获取展示选品库类型",consumes = "application/x-www-form-urlencoded", position = 3, notes = "9趣猫获取展示选品库类型,15元补贴或者3元无门槛", response = RSPE.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
 			@ApiImplicitParam(name = "pid", value = "登录返回字段", example = "5516603495", dataType = "String", paramType = "form"),
 			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001",required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(pid_uuid_pid_utoken_time)", example = "8f85345627bf783e6008f84fac27dd18", required = true, dataType = "String", paramType = "form")})
	@CheckingMD5(checkingStr = "uuid_pid_pid_utoken", MD5 = "token")
	@CheckDevPermission
	public Object getFavoritesType(Integer rtype,String pid, String uuid, String utoken,String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Boolean>(-2, "应用平台参数错误！");
		}
		/* 查看是否存在 */
		CatDevUserInfo zdev = catDevService.getCatDevUserInfoByPid(pid);
		if (zdev==null) {
			return new RSPE<Boolean>(-4, "用户信息不存在错误！");
		}
		
		/* 查看用户今日已领取次数*/
		Callable<RSPE<Integer>> result = new Callable<RSPE<Integer>>() {
			@Override
			public RSPE<Integer> call() throws Exception {
				return new RSPE<Integer>(catOrderService.getFavoritesType(rtype,pid));
			}
		};
		return result;
	}
	
	@PostMapping("/back/info")
	@ApiOperation(value = "9趣猫找回订单记录", notes = "进入找回订单页面,展示填单记录", consumes = "application/x-www-form-urlencoded", position = 4, response = CatDevOrderResp.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_pid_utoken_uuid_pid_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckDevPermission
	@CheckingMD5(checkingStr = "rtype_pid_utoken_uuid_pid", MD5 = "token")
	public Object getBackOrderList(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-3, "应用平台参数错误！");
		}
		Callable<RSPE<List<CatDevOrderResp>>> result = new Callable<RSPE<List<CatDevOrderResp>>>() {
			@Override
			public RSPE<List<CatDevOrderResp>> call() throws Exception {
				return new RSPE<List<CatDevOrderResp>>(catOrderService.getBackOrderList(pid));
			}
		};
		return result;
	}
	
	@PostMapping("/backcash/receive")
	@ApiOperation(value = "9趣猫用户领取返现红包", notes = "9趣猫用户进入首页领取推送的返现红包", consumes = "application/x-www-form-urlencoded", position = 5, response = CatDevOrderResp.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_pid_utoken_uuid_pid_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckDevPermission
	@CheckingMD5(checkingStr = "rtype_pid_utoken_uuid_pid", MD5 = "token")
	public Object receiveBackCash(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-3, "应用平台参数错误！");
		}
		//判断今日是否已领返现红包
		List<CatDevReRedHistory> list = catDevReRedHistoryRepository.findByPidAndFstatusAndPtype(pid, 0,0);
		//判断查询到的返现红包是否已过期
		if (list!=null) {
			list=list.stream().filter(obj->TJKUtils.parse2LocalDateTime(obj.getCtime()).plusDays(1).isAfter(LocalDateTime.now()))
					.collect(Collectors.toList());
		}
		if (list!=null && list.size()>0) {
			return new RSPE<Object>(-4, "已领取返现红包,请勿重复领取！");
		}
		Callable<RSPE<Boolean>> result = new Callable<RSPE<Boolean>>() {
			@Override
			public RSPE<Boolean> call() throws Exception {
				if (catOrderService.receiveBackCash(pid)) {
					return new RSPE<Boolean>(1,"领取成功");
				}else {
					return new RSPE<Boolean>(0,"领取失败");
				}
			}
		};
		return result;
	}
	
	@PostMapping("/backcash/list")
	@ApiOperation(value = "9趣猫用户获取返现红包列表", notes = "9趣猫用户获取返现红包", consumes = "application/x-www-form-urlencoded", position = 5, response = CatDevReRedHistoryResp.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "fstatus", value = "使用状态（0.未使用、1.已使用、2.已过期）", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_pid_utoken_fstatus_uuid_pid_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckDevPermission
	@CheckingMD5(checkingStr = "rtype_pid_utoken_fstatus_uuid_pid", MD5 = "token")
	public Object getBackCashList(Integer rtype, String pid,Integer fstatus,String utoken, String uuid, String token,Pages pages) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-3, "应用平台参数错误！");
		}
		if (fstatus==null || fstatus<0 || fstatus>2) {
			return new RSPE<Object>(-4, "状态参数错误！");
		}
		Callable<RSPE<ResultPage<CatDevReRedHistoryResp>>> result = new Callable<RSPE<ResultPage<CatDevReRedHistoryResp>>>() {
			@Override
			public RSPE<ResultPage<CatDevReRedHistoryResp>> call() throws Exception {
				return new RSPE<ResultPage<CatDevReRedHistoryResp>>(catOrderService.getBackCashList(pid,fstatus,pages));
			}
		};
		return result;
	}
	
	@PostMapping("/promote/rebate")
	@ApiOperation(value = "用户提升返佣比例接口", notes = "用户通过分享朋友圈提升返佣比例", consumes = "application/x-www-form-urlencoded", position = 5, response = CatDevOrderResp.class, responseContainer = "List")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "oid", value = "提升订单ID", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_pid_utoken_uuid_oid_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckDevPermission
	@CheckingMD5(checkingStr = "rtype_pid_utoken_uuid_oid", MD5 = "token")
	public Object promoteRebate(Integer rtype, String pid,String oid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-3, "应用平台参数错误！");
		}
		//查询订单是否存在
		CatDevOrderResp orderResp = orderRespDao.selectOne(CatDevOrderResp.builder().pid(pid).oid(oid).build(), "selectOrder");
		if (orderResp==null) {
			return new RSPE<Object>(-4, "订单不存在！");
		}
		if (orderResp.getPstatus()==1) {
			return new RSPE<Object>(-5, "此订单已提升返利！");
		}
		if (orderResp.getDstatus()==2) {
			return new RSPE<Object>(-6, "订单返利已入账，无法进行返利提升！");
		}
		Callable<RSPE<Boolean>> result = new Callable<RSPE<Boolean>>() {
			@Override
			public RSPE<Boolean> call() throws Exception {
				if (catOrderService.promoteRebate(pid,oid)) {
					return new RSPE<Boolean>(1,"提升成功");
				}else {
					return new RSPE<Boolean>(0,"提升失败");
				}
			}
		};
		return result;
	}
	
	@PostMapping("/rebate/info")
	@ApiOperation(value = "用户获取订单返利信息接口", notes = "用户订单中心,获取用户返利的信息", consumes = "application/x-www-form-urlencoded", position = 5, response = CatDevRebateInfoResp.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户登录时的返回字段", example = "5380881829", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "用户登录时的返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_pid_utoken_uuid_rtype_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckDevPermission
	@CheckingMD5(checkingStr = "rtype_pid_utoken_uuid_rtype", MD5 = "token")
	public Object getRebateInfo(Integer rtype, String pid, String utoken, String uuid, String token) {
		if (rtype == null || (rtype != 0 && rtype != 1)) {
			return new RSPE<Object>(-3, "应用平台参数错误！");
		}
		String userInfoByPid = cacheService.getCatDevUserInfoByPid(pid);
		if (userInfoByPid==null) {
			return new RSPE<Object>(-4, "用户不存在！");
		}
		Callable<RSPE<CatDevRebateInfoResp>> result = new Callable<RSPE<CatDevRebateInfoResp>>() {
			@Override
			public RSPE<CatDevRebateInfoResp> call() throws Exception {
				return new RSPE<CatDevRebateInfoResp>(catOrderService.getRebateInfo(pid));
			}
		};
		return result;
	}
}
